package edu.neu.ecommerceplatform.service.Impl;

import edu.neu.ecommerceplatform.Exception.DuplicateResourceException;
import edu.neu.ecommerceplatform.Exception.ResourceNotFoundException;
import edu.neu.ecommerceplatform.mapper.CategoryMapper;
import edu.neu.ecommerceplatform.mapper.ProductMapper;
import edu.neu.ecommerceplatform.model.Category;
import edu.neu.ecommerceplatform.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    private static final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
    private final CategoryMapper categoryMapper;

    @Autowired
    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }


    @Override
    public List<Category> getCategoryByParentCategoryID(Long id) {
        logger.info("开始查询父分类ID{}的分类",id);

        List<Category> categories =  categoryMapper.findCategoryByParentCategoryID(id);

        if(categories == null){
            logger.warn("未找到父ID{}的子分类",id);
            throw new ResourceNotFoundException("Category","id",""+ id);
        }
        logger.info("成功找到父id{}的分类",id);

        return categories;


    }

    @Override
    public List<Category> getAll() {
        logger.info("查询所有category中");

        List<Category> categories = categoryMapper.findAll();
        if (categories == null){
            logger.warn("没有找到任何Category");
            throw  new ResourceNotFoundException("Category","all","");
        }
        logger.info("成功查询所有category");
        return categories;
    }
}
